<?php
/**
 * Created by PhpStorm.
 * User: s.utkin
 * Date: 14.10.14
 * Time: 13:41
 */

namespace Jobs\Resources;


use Resources\Queue;

class PublishAction extends \Action{

	/**
	 * Проверяет очередь уведомлений о публикации ресурсов и публикует пакеты ресурсов.
	 */
	protected function run()
	{
		$this->response->noRender(true);
		$this->response->noCache(true);
		
		$startTime = time();

		while ((time() - $startTime) < 60) {

			/** @var Queue[] $items */
			$items = Queue::dequeue(100);
			$response = $this->action('resources.workers.publishPackage', ['items' => $items]);

			foreach ((array)$items as $item) {
				if (in_array((string)$item, $response->successList)) {
					$item->remove();
				} else {
					$item->pid = 0;
					$item->save();
				}
			}

			sleep(1);
		}
	}
}